/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：NavCfgDaoImpl.java
 * History:
 *         2012-6-26: Initially created, Chrise.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.BaseDao;
import com.xteam.ecsystem.dao.INavCfgDao;
import com.xteam.ecsystem.entities.SysNavConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 导航配置数据访问实现。
 * @author Chrise
 */
@Repository
public class NavCfgDaoImpl implements INavCfgDao {
	
	@Autowired
	private BaseDao<SysNavConfig> baseDao;
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#getConfigs(int, java.lang.String, int, int, int, int, int)
	 */
	public PageCenterStation<SysNavConfig> getConfigs(int type, String name, 
		int page, int position, int state, int first, int max) {
		String cdt = "";
		if (type != INVALID_TYPE) cdt = " where sncType = " + type;
		if (name != INVALID_NAME) {
			if ("".equals(cdt)) {
				cdt = " where sncName like '%" + name + "%'";
			} else {
				cdt += " and sncName like '%" + name + "%'";
			}
		}
		if (page != INVALID_PAGE) {
			if ("".equals(cdt)) {
				cdt = " where sncPage = " + page;
			} else {
				cdt += " and sncPage = " + page;
			}
		}
		if (position != INVALID_POSITION) {
			if ("".equals(cdt)) {
				cdt = " where sncPosition = " + position;
			} else {
				cdt += " and sncPosition = " + position;
			}
		}
		if (state != INVALID_STATE) {
			if ("".equals(cdt)) {
				cdt = " where sncState = " + state;
			} else {
				cdt += " and sncState = " + state;
			}
		}
		
		String countHql = "select count(sncId) from SysNavConfig" + cdt;
		String queryHql = "from SysNavConfig" + cdt;
		
		return this.baseDao.queryPage(countHql, queryHql, first, max);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#updateState(java.lang.String, int)
	 */
	public void updateState(String id, int state) {
		String hql = "update SysNavConfig set sncState = " + state + " where sncId = '" + id + "'";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#deleteConfigs(java.util.List)
	 */
	public void deleteConfigs(List<String> ids) {
		String cdt = "'" + ids.get(0) + "'";
		for (int index = 1; index < ids.size(); index ++) {
			cdt += ", '" + ids.get(index) + "'";
		}
		
		String hql = "delete from SysNavConfig where sncId in (" + cdt + ")";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#isExist(java.lang.String)
	 */
	public boolean isExist(String name) {
		String hql = "select count(sncId) from SysNavConfig where sncName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#isExist(java.lang.String, java.lang.String)
	 */
	public boolean isExist(String id, String name) {
		String hql = "select count(sncId) from SysNavConfig where sncId <> '" + id + 
			"' and sncName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#insertConfig(com.xteam.ecsystem.entities.SysNavConfig)
	 */
	public void insertConfig(SysNavConfig snc) {
		this.baseDao.save(snc);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#updateConfig(com.xteam.ecsystem.entities.SysNavConfig)
	 */
	public void updateConfig(SysNavConfig snc) {
		this.baseDao.update(snc);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#getConfig(java.lang.String)
	 */
	public SysNavConfig getConfig(String id) {
		String hql = "from SysNavConfig where sncId = '" + id + "'";
		return this.baseDao.findUniqueObject(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.INavCfgDao#getNavigations(int, int, int, int)
	 */
	public List<SysNavConfig> getNavigations(int common, int page, int position, int state) {
		String cdt = " and sncPage in (" + common;
		if (page != INVALID_PAGE) {
			cdt += ", " + page;
		}
		cdt += ")";
		if (position != INVALID_POSITION) {
			cdt += " and sncPosition = " + position;
		}
		String hql = "from SysNavConfig where sncState = " + state + cdt + " order by sncOrder desc";
		
		return this.baseDao.queryList(hql);
	}
}
